window.onload = function(){
// 1.获取Dom
// 全选按钮
let All = document.querySelector('.checkAll');
let Alls = document.querySelector('.smailBox');
// 子复选框
let  radio = document.querySelectorAll('.radio');
// 总金额
let  figure =document.querySelector('.figure');
//删除
let del = document.querySelectorAll('.delet');
//单价
let prices = document.querySelectorAll('.tbody_unitPrice');
// 输入框Dom
let num = document.querySelectorAll('.num');
// 获取小计Dom
let subtotal = document.querySelectorAll('.tbody_subtotal');
// 获取+btn
let plus = document.querySelectorAll('.plus');
// 获取-Btn
let minus = document.querySelectorAll('.minus');
let digit =document.querySelector('.digit');
// 封装函数
function   Money(){
    let  radio = document.querySelectorAll('.radio');
    let subtotal = document.querySelectorAll('.tbody_subtotal');
    let num = document.querySelectorAll('.num');
    let Num = 0;
    let Money = 0;
    for(let i = 0;i<radio.length;i++){
           if(radio[i].checked === true){
        Num = Num + parseFloat(num[i].value);
        Money = Money + parseFloat(subtotal[i].innerText);
    }
}
digit.innerText = Num;
figure.innerText = Money;

}




// 2.全选选中
All.addEventListener('click',function(){
    if(All.checked){
        for(let i = 0;i<radio.length;i++){
            radio[i].checked = true;
            Alls.checked = All.checked;
        }
    }else{
        for(let j = 0;j<radio.length;j++){
            radio[j].checked = false;
            Alls.checked = All.checked;
        }
    }
    Money();
})
Alls.addEventListener('click',function(){
    if(Alls.checked){
        for(let i = 0;i<radio.length;i++){
            radio[i].checked = true;
            All.checked = Alls.checked;
        }
    }else{
        for(let j = 0;j<radio.length;j++){
            radio[j].checked = false;
            All.checked = Alls.checked;
        }
    }
    Money()
})

// 3.复选框
for(let i = 0; i<radio.length;i++){
    radio[i].addEventListener('click',function(){
     let status = true;
     for(let j = 0; j<radio.length;j++){
         if(radio[j].checked ===false){
            status = false;
         }
     }
     All.checked = status;
     Alls.checked = status;
     Money();
    })
}
// 4.加减
for(let i = 0;i< plus.length;i++){
    plus[i].addEventListener('click',function(){
        num[i].value++;
        subtotal[i].innerText = parseInt(num[i].value)*parseFloat(prices[i].innerText);
        Money();
    })
}
for(let i = 0;i< minus.length;i++){
    minus[i].addEventListener('click',function(){
        num[i].value--;
        if(num[i].value<1){
            num[i].value = 1;
        }
        subtotal[i].innerText = parseInt(num[i].value)*parseFloat(prices[i].innerText);
        Money();
    })
}

// 5.输入框
for(let i = 0;i<num.length;i++){
    num[i].addEventListener('change',function(){
        if(num[i].value!=Number(num[i].value)){
            alert('请输入数字')
        }
        subtotal[i].innerText = parseInt(num[i].value)*parseFloat(prices[i].innerText);
        Money();
    })
}
// 6.删除
for(let i = 0;i<del.length;i++){
    del[i].addEventListener("click",function(){
       console.log(del[i].parentNode.parentNode);
       del[i].parentNode.parentNode.remove();
    })
}



}